package com.kun.utils.VertifyCode;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @Version:v1.0
 * @Author: 鲁坤
 * @Date: 2021-02-09
 * @ClassName: CheckCode
 * @Description: 验证码功能
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CheckCode {
    //从codeSource中随机选择四个字符
    private final static String codeSource="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private String code;//验证码
    private int codenumbers;//验证码长度
    private int fontsize;//字体大小
    private int corruptline;//干扰线数量
    //生成验证码
    public String produceCode(){
        code=null;
        Random r = new Random();
        /*for(int i=0;i<codenumbers;i++){
            code+=codeSource.charAt(r.nextInt(61));
        }*/
        int code1= r.nextInt(61);
        int code2= r.nextInt(61);
        int code3= r.nextInt(61);
        int code4= r.nextInt(61);
        code=""+codeSource.charAt(code1)+codeSource.charAt(code2)+codeSource.charAt(code3)+codeSource.charAt(code4);
        return code;
    }
    //绘制验证码图片
    public Object[] createImage(){
        String securitycode = this.produceCode();
        int codelength = securitycode.length();//验证码长度
        int fontWidth = fontsize+1;//验证码字间距
        //图片宽高
        int width = (codelength+2)*fontWidth+6;
        int height = fontsize*2-3;

        int codeX = (width-4) / (codelength+1);
        int codeY = height-12;
        //图片画布
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

        graphics.setColor(Color.WHITE);//设置背景色
        graphics.fillRect(0,0,width,height);//填充背景

        graphics.setFont(new Font("Times New Roman", Font.PLAIN,height-5));//设置字体样式
        graphics.setColor(Color.orange);//设置边框颜色
        graphics.drawRect(0,0,width-1,height-1);//绘制边框

        //绘制干扰线
        Random random = new Random();
        graphics.setColor(Color.GRAY);//设置干扰线颜色
        for (int i = 0; i < corruptline; i++) {
            int xs = random.nextInt(width);//干扰线起点横坐标
            int ys = random.nextInt(height);//干扰线起点纵坐标
            int xd = random.nextInt(12);//干扰线终点横坐标
            int yd = random.nextInt(12);//干扰线终点纵坐标
            graphics.drawLine(xs,ys,xs+xd,ys+yd);//绘制直线
        }
        //绘制具体字符
        int red=0,green=0,blue=0;//RGB三通道数值
        double deg = 0.0;   //旋转角度
        for (int i = 0; i < codelength; i++) {
            red=random.nextInt(255);
            green=random.nextInt(255);
            blue=random.nextInt(255);
            deg= random.nextDouble()*20;
            graphics.setColor(new Color(red,green,blue));
            graphics.rotate(Math.toRadians(deg), i * 16 + 13, codeY - 7.5);//控制旋转
            graphics.drawString(String.valueOf(securitycode.charAt(i)),(i)*codeX+10,codeY);
            graphics.rotate(Math.toRadians(-deg), i * 16 + 13, codeY - 7.5);
        }
        graphics.dispose();
        return new Object[]{securitycode, image};
    }
}
